//
//  718. 最长重复子数组.swift
//  Top600
//
//  Created by rjb on 2021/7/30.
//

import Foundation
class Solution718 {
    // 定义dp[i][j] 以i，j结尾的最长公共子数组
    func findLength(_ nums1: [Int], _ nums2: [Int]) -> Int {
        var dp: [[Int]] = Array(repeating: Array(repeating: 0, count: nums2.count + 1), count: nums1.count + 1)
        var result: Int = 0
        for i in 1...nums1.count {
            for j in 1...nums2.count {
                // 如果相等
                if nums1[i - 1] == nums2[j - 1] {
                    dp[i][j] = dp[i - 1][j - 1] + 1
                }
                result = max(result, dp[i][j])
            }
        }
        return result
    }
    static func test() {
        let solution = Solution718()
        let nums1 = [1,2,3,2,1]
        let nums2 = [3,2,1,4,7]
        let result = solution.findLength(nums1, nums2)
        print(result)
    }
}
